import { dataType } from "element-plus/es/components/table-v2/src/common.mjs";
import axios from "axios";
const baseURL = "http://localhost:8088";
axios.defaults.baseURL = baseURL;
const Request = {
  doGet(url, params) {
    return axios({
      url: baseURL + url,
      method: "GET",
      params: params,
      responseType: "json",
    })
      .then(function (response) {
        return response.data;
      })
      .catch(function (error) {
        throw new Error(error);
      });
  },

  doPost(url, params, data) {
    return axios({
      url: baseURL + url,
      method: "POST",
      params: params,
      data: data,
      responseType: "json",
    })
      .then(function (response) {
        return response.data;
      })
      .catch(function (error) {
        throw new Error(error);
      });
  },

  doPut(url, params, data) {
    return axios({
      url: baseURL + url,
      method: "PUT",
      params: params,
      data: data,
      responseType: "json",
    })
      .then(function (response) {
        return response.data;
      })
      .catch(function (error) {
        throw new Error(error);
      });
  },

  doDelete(url, params) {
    return axios({
      url: baseURL + url,
      method: "DELETE",
      params: params,
      responseType: "json",
    })
      .then(function (response) {
        return response.data;
      })
      .catch(function (error) {
        throw new Error(error);
      });
  },
};
axios.interceptors.request.use(
    function (config) {
      let token = localStorage.getItem("dlyk_token") || sessionStorage.getItem("dlyk_token");
      
      if (localStorage.getItem("dlyk_token")) {
        config.headers["rememberMe"] = true;
      }
      
      config.headers["Authorization"] = token;
      return config;
    },
    function (error) {
      return Promise.reject(error);
    }
  );
  
  axios.interceptors.response.use(
    function (response) {
      if (response.data.code > 900) {
        ElMessageBox.confirm(response.data.msg, "Warning", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            utils.removeToken();
            window.location.href = "/";
          })
          .catch(() => {
            ElMessage({
              type: "warning",
              message: "取消登录",
            });
            utils.removeToken();
          });
          return
      }
      return response;
    },
    function (error) {
      return Promise.reject(error);
    }
  );

import { ElMessage, ElMessageBox } from "element-plus";
import utils from "../utils/utils";

export default Request;
export {
  baseURL
}
